<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="公司" prop="companyid">
        <el-select v-model="queryParams.companyid" placeholder="请选择公司" filterable clearable size="small">
          <el-option
            v-for="item in orgOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
            @click.native="getStorerooms($event, item)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库房" prop="srguid">
        <el-select v-model="queryParams.srguid" placeholder="请选择库房" @change="findStoreroomhouse" filterable clearable size="small">
          <el-option
            v-for="item in storeroomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库间" prop="shguid">
        <el-select v-model="queryParams.shguid" placeholder="请选择库间" filterable clearable size="small">
          <el-option
            v-for="item in storehouseroomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品类型" prop="">
        <el-select v-model="queryParams.type" placeholder="请选择产品类型" filterable clearable size="small">
          <el-option
            v-for="item in productTypes"
            :key="item.dictkey"
            :label="item.dicttext"
            :value="item.dictkey"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品" prop="productid">
        <el-select v-model="queryParams.productid" placeholder="请选择产品" size="small" filterable clearable>
          <el-option
            v-for="item in productOptions"
            :key="item.guid"
            :label="item.productname"
            :value="item.guid"
            @click.native="getSp($event, item)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="规格" prop="specid">
        <el-select v-model="queryParams.specid" placeholder="请选择规格" filterable size="small" clearable>
          <el-option
            v-for="item in spOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起止时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          unlink-panels
        ></el-date-picker>
      </el-form-item>
      <br/>
      <div>
        <el-form-item label="汇总: " prop="byday">
          <el-select v-model="queryParams.othercondition" placeholder="请选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in field103Options" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['repository:storeanalysis:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--    <el-table v-loading="loading" :data="storeanalysisList" border @selection-change="handleSelectionChange" style="max-height: 260px;overflow-y: auto;">-->
    <el-table v-loading="loading" :data="storeanalysisList" border @selection-change="handleSelectionChange" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="库房" align="center" prop="srname" />
      <el-table-column label="库间" align="center" v-if="storeanalysisList[0] != undefined && storeanalysisList[0].shname != undefined && (queryParams.othercondition== 'sh' || queryParams.othercondition== 'product' || queryParams.othercondition== 'sp') " prop="shname" />
<!--      <el-table-column label="公司" align="center" v-if="storeanalysisList[0] != undefined && storeanalysisList[0].companyname != '' && showCompany  "  prop="companyname" />-->
<!--      <el-table-column label="产品" align="center"  v-if="queryParams.othercondition!= ''" prop="productname" />-->
<!--      <el-table-column label="规格" align="center"  v-if="queryParams.othercondition!= ''" prop="specname" />-->
      <el-table-column label="产品" align="center" prop="productname" />
      <el-table-column label="规格" align="center" prop="specname" />
      <el-table-column label="期初库存" align="center" v-if="queryParams.byday == ''" prop="startstore" />
      <el-table-column label="日期" align="center" v-if="queryParams.byday != ''" prop="rptdate" />
      <el-table-column label="入库" align="center" prop="inWeightSUM"/>
      <el-table-column label="出库" align="center" prop="outWeightSUM"/>
<!--      <el-table-column label="采收入库" align="center" prop="buyinweightSUM" />-->
<!--      <el-table-column label="生产入库" align="center" prop="prodinweightSUM" />-->
<!--      <el-table-column label="调整入库" align="center" prop="adjinweightSUM" />-->
<!--      <el-table-column label="退料入库" align="center" prop="retinweightSUM" />-->
<!--      <el-table-column label="调拨入库" align="center" prop="transinweightSUM" />-->
<!--      <el-table-column label="领料出库" align="center" prop="pickoutweightSUM" />-->
<!--&lt;!&ndash;      <el-table-column label="生产报废" align="center" prop="pscrapweight" />&ndash;&gt;-->
<!--      <el-table-column label="销售出库" align="center" prop="selloutweightSUM" />-->
<!--      <el-table-column label="调拨出库" align="center" prop="transoutweightSUM" />-->
<!--      <el-table-column label="调整出库" align="center" prop="adjoutweightSUM" />-->
<!--      <el-table-column label="腐烂报废" align="center" prop="scrbadoutweightSUM" />-->
<!--      <el-table-column label="库损报废" align="center" prop="scrlostoutweightSUM" />-->
      <el-table-column label="期末库存" align="center" prop="endstore" />
      <el-table-column label="库存单位" align="center" prop="stockunit" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>


<script>
import { listReportStoreanalysis, getStoreanalysis, delStoreanalysis, addStoreanalysis, updateStoreanalysis, exportStoreanalysis } from "@/api/repository/storeanalysis";
import {findOrganizationList, findProductList, findSpecificationList, findStorehouse} from "@/api/public";
import { findStoreroomListByPermiss } from "@/api/public";
import { getOldDicts } from "@/api/system/dict/data";
export default {
  name: "Storeanalysis",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动态库存表格数据
      storeanalysisList: [],
      orgOptions: [],
      storeroomOptions: [],
      storehouseroomOptions: [],
      productTypes: [],
      productOptions: [],
      spOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        srtype: 1,
        srguid: null,
        type:null,
        shguid: null,
        companyid: null,
        productid: null,
        specid: null,
        byday: "",
        ordercondition: "",
        othercondition : ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dateRange: [],
      field103Options: [{
        "label": "库房",
        "value": "sr"
      }, {
        "label": "库间",
        "value": "sh"
      }
      ]
    };
  },
  created() {
    // this.getList();
    this.initQuerys();

    let firDate = this.getNowMonthDate();
    let curDate = this.getDate(0);
    this.dateRange = [new Date(firDate[0], firDate[1], firDate[2]), new Date(curDate[0], curDate[1], curDate[2])]
  },
  methods: {
    //获取当前日期
    getDate: function (dayCount = 0) {
      var dd = new Date();
      dd.setDate(dd.getDate()+dayCount);//设置日期
      var y = dd.getFullYear();
      var m = dd.getMonth();//获取当前月份的日期
      var fullM = m.length == 2 ? m : '0' + m;
      var d = dd.getDate();
      // return y+"-"+fullM+"-"+d;
      return [y, fullM, d];
    },
    //获取本月第一天
    getNowMonthDate: function () {
      var date = new Date();
      var year = date.getFullYear() + "";
      var month = date.getMonth() + "";
      // 本月第一天日期
      // var begin = year + "-" + month + "-01"
      return [year, month, "01"];
    },
    /** 查询动态库存列表 */
    getList() {
      this.loading = true;
      this.addTimeRange(this.queryParams, this.dateRange);
      if (this.queryParams.type==''){
        this.queryParams.type=null
      }
      if (this.queryParams.shguid==''){
        this.queryParams.shguid=null
      }
      if (this.queryParams.srguid==''){
        this.queryParams.srguid=null
      }
      if (this.queryParams.companyid==''){
        this.queryParams.companyid=null
      }
      if (this.queryParams.productid==''){
        this.queryParams.productid=null
      }
      if (this.queryParams.specid==''){
        this.queryParams.specid=null
      }
      listReportStoreanalysis(this.queryParams).then(response => {
        //   this.storeanalysisList = response.data.data.filter(item => {
        //     return item.startstore != '' || item.buyinweightSUM != '' || item.prodinweightSUM != '' || item.adjinweightSUM != '' || item.retinweightSUM != '' || item.transinweightSUM != '' || item.pickoutweightSUM != '' || item.selloutweightSUM != '' || item.adjoutweightSUM != '' || item.transoutweightSUM != '' || item.scrbadoutweightSUM != '' || item.scrlostoutweightSUM != '' || item.endstore != '';
        //   });
        //   if (response.data.data.length>0){
        //     this.total=response.data.data.length;
        //   }
        //   this.loading = false;
        // });

        this.storeanalysisList=response.rows;
        this.loading=false;
        this.total=response.total;
      })
    },
    addTimeRange(params, dateRange) {
      var search = params;
      search.starttime = "";
      search.endtime = "";
      if (null != dateRange && '' != dateRange) {
        search.starttime = dateRange[0];
        search.endtime = dateRange[1];
      }
      return search;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        rptdate: null,
        srguid: null,
        srname: null,
        shguid: null,
        shname: null,
        companyid: null,
        companyname: null,
        productid: null,
        productname: null,
        specid: null,
        specname: null
      };
      this.resetForm("form");
    },
    initQuerys(){
      findOrganizationList().then(response => {
        this.orgOptions = response.data;
      });
      /*findStoreroomListByPermiss({functionType:10}).then(response => {
        this.storeroomOptions = response.data;
      });*/
      getOldDicts({dicttype:91}).then(response => {
        this.productTypes = response.data;
      });
      findProductList().then(response => {
        this.productOptions = response.data;
      });
    },
    getStorerooms(event, item){
      findStoreroomListByPermiss({functionType:10, orgguid: item.guid}).then(response => {
        this.storeroomOptions = response.data;
      });
    },
    getSp(event, item){
      findSpecificationList({productguid:item.guid}).then(response => {
        this.spOptions = response.data;
      });
    },
    /** 查询库间信息列表 */
    findStoreroomhouse(query) {
      findStorehouse({storeguid:query}).then(response => {
        this.storehouseroomOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动态库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getStoreanalysis(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动态库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateStoreanalysis(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreanalysis(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStoreanalysis(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有动态库存数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStoreanalysis(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
